var _INDEX = '0';
var _TYPE = 'MerchantWords:Search P1 Keyword List';

function log(msg,level = config.LOG._LOG_LEVEL_DEBUG) {
    logger.out(`{${_TYPE} (index:${_INDEX})} ` + msg,level)
}

function parser() {
    let result = {
        rows :{
            topInfo:[],
            list:[]
        },
        code:Const._RETCODE_SUCCESS
    };

    //解析头部数据
    result.rows.topInfo = parserTopInfo();
    result.rows.list = parserList();
    return result;
}

function parserList() {
    let result = [];
    let list = $('#resultsTable > tbody > tr');
    if(!list || list.length === 0){
        return result;
    }
    for(let i = 0; i < list.length; i++){
        let dom = list[i];
        let keyword = parserKeyword(dom);
        let href = parserHref(dom);
        let title = parserTitle(dom);
        let image = parserImage(dom);
        let position = $(dom).find('td:nth-child(3)').text();
        let price = $(dom).find('td:nth-child(4)').text();
        let reviews = $(dom).find('td:nth-child(5)').text();
        let rating = $(dom).find('td:nth-child(6)').text();
        let choice = $(dom).find('td:nth-child(7)').text();
        result.push({
            keyword:keyword,
            href:href,
            title:utils.special(title),
            image:image,
            position:position,
            price:price,
            reviews:reviews,
            rating:rating,
            choice:choice
        })
    }
    return result;
}

function parserImage(dom) {
    //#resultsTable > tbody > tr:nth-child(1) > td:nth-child(2) > div > span
    let tdom = $(dom).find('td:nth-child(2) > div > span > img');
    return $(tdom).attr('src');
}

function parserTitle(dom) {
    //#resultsTable > tbody > tr:nth-child(1) > td:nth-child(2) > div > span
    let tdom = $(dom).find('td:nth-child(2) > div > span > b');
    return $(tdom).text();
}

function parserHref(dom) {
    let adom = $(dom).find('td.table__col-size5 > div > div > div > ul > li.keywords > span > a')
    return $(adom).attr('href');
}

function parserKeyword(dom) {
    //#resultsTable > tbody > tr:nth-child(1) > td.table__col-size5 > div > div > div > ul > li.keywords > span > a
    let adom = $(dom).find('td.table__col-size5 > div > div > div > ul > li.keywords > span > a')
    return $(adom).text();
}

function parserTopInfo() {
    let result = [];
    let list = $('body > div.wrapper.wrapper--alt > section > div.container > header > div:nth-child(2) > div > div.table.table--summary > table > tbody > tr > td');
    log(`P1 List Length:${list.length}`);
    if(!list || list.length === 0){
        return result;
    }
    for(let i = 0; i < list.length; i++){
        let dom = $(list[i]);
        result.push($(dom).text())
    }
    return result;
}

try {
    var $ = cheerio.load(htmlString);
    result = parser();
    if(result.rows){
        result.code =Const._RETCODE_SUCCESS;
    }

    parserInfo.index = _INDEX;
    parserInfo.type = _TYPE;
} catch (e) {
    log(`Parsing Exception:${e.message}`,config.LOG._LOG_LEVEL_ERROR);
    result.code = Const._RETCODE_HANDLER_FAILED;
}

